package net.oschina.dachuan.kryoserializers;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.net.URI;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Currency;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.TreeSet;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

import de.javakaffee.kryoserializers.BitSetSerializer;
import de.javakaffee.kryoserializers.EnumMapSerializer;
/**
 * 
 * 
 * @author 张大川
 * 
 * 测试 kryo 4.0
 *
 */
public class SerializerTest {
	public static <V> void main(String[] args) throws FileNotFoundException {
		EnumSet<Month> enumSet = EnumSet.noneOf(Month.class);
		enumSet.add(Month.April);
		enumSet.add(Month.March);
		Kryo kryo = new Kryo();

		FileOutputStream fileOutputStream = new FileOutputStream("xxx1");
		Output output = new Output(fileOutputStream);
		kryo.writeObject(output, enumSet);// 4.0 原生支持 EnumSet 序列化 4.0 之前不支持
		output.close();

		Input input = new Input(new FileInputStream("xxx1"));
		EnumSet<Month> someObject = kryo.readObject(input, EnumSet.class);
		System.out.println(someObject.toString());
		input.close();

		EnumMap<Month, String> map = new EnumMap<Month, String>(Month.class);
		// put方法
		map.put(Month.January, "1");
		map.put(Month.February, "2");
		map.put(Month.March, "3");
		FileOutputStream fileOutputStream1 = new FileOutputStream("xxx2");
		Output output1 = new Output(fileOutputStream1);
		kryo.register(EnumMap.class, new EnumMapSerializer());// 4.0 不支持 enummap
		kryo.writeObject(output1, map);
		output1.close();
		Input input1 = new Input(new FileInputStream("xxx2"));
		EnumMap<Month, String> obMap = kryo.readObject(input1, EnumMap.class);
		System.out.println(obMap.toString());
		input.close();

		Calendar c = Calendar.getInstance();
		FileOutputStream fileOutputStream11 = new FileOutputStream("xxx21");
		Output output11 = new Output(fileOutputStream11);
		kryo.writeObject(output11, c);

		output11.close();
		Input input11 = new Input(new FileInputStream("xxx21"));
		Calendar obMap1 = kryo.readObject(input11, Calendar.class);
		System.out.println(obMap1.toString());
		input11.close();

		BitSet bm = new BitSet();
		bm.set(64);
		FileOutputStream fileOutputStream12 = new FileOutputStream("xx");
		Output output12 = new Output(fileOutputStream12);
		kryo.register(BitSet.class, new BitSetSerializer());// 使用外部添加的序列化器
		kryo.writeObject(output12, bm);

		output12.close();
		Input input12 = new Input(new FileInputStream("xx"));
		BitSet obMap12 = kryo.readObject(input12, BitSet.class);
		System.out.println(obMap12.toString());
		input12.close();

		TreeSet<String> w = new TreeSet<String>();

		w.add("sss");
		w.add("xxx");
		FileOutputStream fileOutpu = new FileOutputStream("xx");
		Output outpu = new Output(fileOutpu);
		// kryo.register(BitSet.class, new BitSetSerializer());
		kryo.writeObject(outpu, w);

		outpu.close();

		Input inputee = new Input(new FileInputStream("xx"));
		TreeSet ob = kryo.readObject(inputee, TreeSet.class);
		System.out.println(ob.toString());
		inputee.close();
		
		
		Currency er= Currency.getInstance("CNY");
		FileOutputStream stream = new FileOutputStream("xx");
		Output output2 = new Output(stream);
		// kryo.register(BitSet.class, new BitSetSerializer());
		kryo.writeObject(output2, er);

		output2.close();
		

		Input inputw= new Input(new FileInputStream("xx"));
		Currency obc = kryo.readObject(inputw, Currency.class);
		System.out.println(obc.toString());
		inputw.close();
		
		
		URI it = URI.create("XXX");
		FileOutputStream stream1 = new FileOutputStream("xx");
		Output output3 = new Output(stream1);
		// kryo.register(BitSet.class, new BitSetSerializer());
		kryo.writeObject(output3, it);//原生支持URI

		output3.close();
		
         
		Input input23= new Input(new FileInputStream("xx"));
		URI uri = kryo.readObject(input23, URI.class);
		System.out.println(uri.toString());
		input23.close();
		
	}
}
